<?php

// Liveuser config
require_once 'config.inc.php';

// Our xml for flex creator object
require_once( "flex_xml.php" );

// Our Common data functions
include_once( "data.inc" );


switch( $_REQUEST['td'] ){
    case "getA":
		if( !Empty( $_REQUEST['action'] ) ){
    		if( $_REQUEST['action'] == "add" ){
		    $admin->perm->addApplication( array( 'application_define_name' => $_REQUEST['data1'] ) );
		} elseif( $_REQUEST['action'] == "del" ){
		    $admin->perm->removeApplication( array( 'application_id' => $_REQUEST['data1'] ) );
		} elseif( $_REQUEST['action'] == "edit" ){
		    $res = $admin->perm->updateApplication( array( 'application_define_name' => $_REQUEST['data2'] ),
			array( 'application_id' => $_REQUEST['data1']  ) ) ;
		}
    		}
    		// Get applications
		$flex_res = $admin->perm->getApplications();
		flex_finish( $flex_res);
        break;
    case "getAr":
		if( !Empty( $_REQUEST['action'] ) ){
    		if( $_REQUEST['action'] == "add" ){
		    $admin->perm->addArea( array( 'area_define_name' => $_REQUEST['data1'],'application_id' => $_REQUEST['data2']  ) );
		} elseif( $_REQUEST['action'] == "del" ){
		    $admin->perm->removeArea( array( 'area_id' => $_REQUEST['data1'] ) );
		} elseif( $_REQUEST['action'] == "edit" ){
		    $admin->perm->updateArea( array( 'area_define_name' => $_REQUEST['data2'] ),
			array( 'area_id' => $_REQUEST['data1']  ) ) ;
		}
    		}
    		// Get areas
		$flex_res = $admin->perm->getAreas();
		flex_finish( $flex_res);
        break;
    case "getU":
	if( !Empty( $_REQUEST['action'] ) ){
    		if( $_REQUEST['action'] == "add" ){
    			//Add default user
		    $admin->addUser( array( 
		    	'handle' => $_REQUEST['data2'],
		    	'name' => $_REQUEST['data3'],
		    	'email' => $_REQUEST['data4'],
		    	'address' => $_REQUEST['data5'],
		    	'tel1' => $_REQUEST['data6'],
		    	'tel2' => $_REQUEST['data7'],
		    	'isactive' => $_REQUEST['data8'],
		    	'passwd' => $_REQUEST['data9'],
		    	'perm_type' => 1
		     ) );
		} elseif( $_REQUEST['action'] == "del" ){
		    $perm = $admin->getUsers( array( 'container' => 'auth', 'filters' => array( 'handle' => $_REQUEST['data1'] ) ) );
		    $admin->perm->removeUser( array( 'perm_user_id' => $perm[0]['perm_user_id'] ) );
		} elseif( $_REQUEST['action'] == "edit" ){
			$perm = $admin->getUsers( array( 'container' => 'auth', 'filters' => array( 'handle' => $_REQUEST['data1'] ) ) );
				$data = array( 
		    	'handle' => $_REQUEST['data2'],
		    	'name' => $_REQUEST['data3'],
		    	'email' => $_REQUEST['data4'],
		    	'address' => $_REQUEST['data5'],
		    	'tel1' => $_REQUEST['data6'],
		    	'tel2' => $_REQUEST['data7'],
		    	'isactive' => $_REQUEST['data8'] );
		    	if( !Empty( $_REQUEST['data9'] ) ){
		    		$data['passwd'] = $_REQUEST['data9'];
				}
		    $admin->updateUser( $data,	array( 'perm_user_id' =>  $perm[0]['perm_user_id'] )
		      ) ;
		}
    	}
	// Get users
	$flex_res = $admin->getUsers();
	flex_finish( $flex_res);
        break;
    case "getG":
	if( !Empty( $_REQUEST['action'] ) ){
    		if( $_REQUEST['action'] == "add" ){
		    $admin->perm->addGroup( array( 'group_define_name' => $_REQUEST['data1'] ) );
		} elseif( $_REQUEST['action'] == "del" ){
		    $admin->perm->removeGroup( array( 'group_id' => $_REQUEST['data1'] ) );
		} elseif( $_REQUEST['action'] == "edit" ){
		    $admin->perm->updateGroup( array( 'group_define_name' => $_REQUEST['data2'] ),
			array( 'group_id' => $_REQUEST['data1']  ) ) ;
		}
    	}    
    	// Get groups
	$flex_res = $admin->perm->getGroups();
	flex_finish( $flex_res);
        break;
    case "getR":
	if( !Empty( $_REQUEST['action'] ) ){
    		if( $_REQUEST['action'] == "add" ){
		    $admin->perm->addRight( array( 'right_define_name' => $_REQUEST['data1'],'area_id' => $_REQUEST['data2'], 'has_implied' => 0 ) );
		} elseif( $_REQUEST['action'] == "del" ){
		    $admin->perm->removeRight( array( 'right_id' => $_REQUEST['data1'] ) );
		} elseif( $_REQUEST['action'] == "edit" ){
		    $admin->perm->updateRight( array( 'right_define_name' => $_REQUEST['data2'] ),
			array( 'right_id' => $_REQUEST['data1']  ) ) ;
		}
    	}    
    	// Get groups
		$flex_res = $admin->perm->getRights();
		flex_finish( $flex_res);
        break;
    case "grant":
	if( !Empty( $_REQUEST['action'] ) ){
    		if( $_REQUEST['action'] == "grantG" ){
		    $flex_res = $admin->perm->grantGroupRight( array( 'right_id' => $_REQUEST['data2'],'group_id' => $_REQUEST['data1'] ) );
		} elseif( $_REQUEST['action'] == "revokeG" ){
		    $flex_res = $admin->perm->revokeGroupRight( array( 'right_id' => $_REQUEST['data2'],'group_id' => $_REQUEST['data1'] ) );
		} elseif( $_REQUEST['action'] == "grantU" ){
		    $perm = $admin->getUsers( array( 'container' => 'auth', 'filters' => array( 'handle' => $_REQUEST['data1'] ) ) );
		    $flex_res = $admin->perm->grantUserRight( array( 'right_id' => $_REQUEST['data2'],'perm_user_id' => $perm[0]['perm_user_id'] ) );
		} elseif( $_REQUEST['action'] == "revokeU" ){
		    $perm = $admin->getUsers( array( 'container' => 'auth', 'filters' => array( 'handle' => $_REQUEST['data1'] ) ) );
		    $flex_res = $admin->perm->revokeUserRight( array( 'right_id' => $_REQUEST['data2'],'perm_user_id' => $perm[0]['perm_user_id'] ) );
		} elseif( $_REQUEST['action'] == "addU" ){
		    $perm = $admin->getUsers( array( 'container' => 'auth', 'filters' => array( 'handle' => $_REQUEST['data1'] ) ) );
		    $flex_res = $admin->perm->addUserToGroup( array( 'group_id' => $_REQUEST['data2'],'perm_user_id' => $perm[0]['perm_user_id'] ) );
		} elseif( $_REQUEST['action'] == "removeU" ){
		    $perm = $admin->getUsers( array( 'container' => 'auth', 'filters' => array( 'handle' => $_REQUEST['data1'] ) ) );
		    $flex_res = $admin->perm->removeUserFromGroup( array( 'group_id' => $_REQUEST['data2'],'perm_user_id' => $perm[0]['perm_user_id'] ) );
		}
		flex_finish( $flex_res );
    	}    
        break;
    case "getAdminTree":
	usleep(200000); //Just the hack... If db updating pending, we have to wait...
	flex_tree();
        break;
    case "getLogin":
    	// Get Liveuser Login info
	    $flex_res =  array(
	    	            'user' => $admin->getProperty('handle'),
			    'lastLogin' => date('d.m.Y H:i', $admin->getProperty('lastlogin'))
			    );
		flex_finish( $flex_res);
        break;
    case "getLog":
    	// Get debug log
	$output = array();
	$return_var = 0;
	$lastline = exec("tail -".$_REQUEST['data1'].' '.escapeshellcmd($logfile), $output, $return_var);
	$flex_res = array( array( 'line'=>implode("\n",$output) ) );
	flex_finish( $flex_res);
        break;


    default:
    	$flex_res = false;
	flex_finish( $flex_res);
        break;
}
?>
